// components/stepper/stepper.js
/* 点击减号 */
function bindMinus(event) {
  let stepperArr = this.data.stepper;
  let index = event.currentTarget.dataset.index;
  let min = event.currentTarget.dataset.min;
  let num = stepperArr['id_' + index].num;
  // 如果大于1时，才可以减  
  if (num > min) {
    num--;
    stepperArr['id_' + index].maxStatus = "normal";
  }
  // 只有大于一件的时候，才能normal状态，否则disable状态  
  let minusStatus = num <= min ? 'disabled' : 'normal';
  // 将数值与状态写回  
  stepperArr['id_' + index].num = num;
  stepperArr['id_' + index].minusStatus = minusStatus;
  this.setData({
    stepper: stepperArr
  });
} 
/* 点击加号 */
function bindPlus(event) {
  let stepperArr = this.data.stepper;
  let index = event.currentTarget.dataset.index;
  let max = event.currentTarget.dataset.max;
  let num = stepperArr['id_' + index].num;
  // 不作过多考虑自增1
  if (num < max){
    num++;
    stepperArr['id_' + index].minusStatus = "normal";
  }
  // 只有大于一件的时候，才能normal状态，否则disable状态  
  let maxStatus = num >= max ? 'disabled' : 'normal';
  // 将数值与状态写回 
  stepperArr['id_' + index].num = num;
  stepperArr['id_' + index].maxStatus = maxStatus;
  this.setData({
    stepper: stepperArr
  });
} 
/* 输入框事件 */
function bindManual(event) {
  let stepperArr=this.data.stepper;
  let num = event.detail.value;
  let index = event.currentTarget.dataset.index;
  let max = event.currentTarget.dataset.max;
  let min = event.currentTarget.dataset.min;
  if (num<min){
    num=min;
    stepperArr['id_' + index].maxStatus = "normal";
    stepperArr['id_' + index].minusStatus = "disabled";
  }
  if(num>max){
    num=max;
    stepperArr['id_' + index].minusStatus = "normal";
    stepperArr['id_' + index].maxStatus = "disabled";
  }
  stepperArr['id_' + index].num = num
  // 将数值与状态写回  
  this.setData({
    stepper: stepperArr
  });
}
function init(stepperArr){
  var that = getCurrentPages()[getCurrentPages().length - 1];//获取当前page实例
  that.setData({
    stepper: stepperArr
  })
  that.bindMinus = bindMinus;
  that.bindManual = bindManual;
  that.bindPlus = bindPlus;
}
module.exports = {
  init: init,
}